/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.edu.fpt.xml.hrm.controller.timesheet;

import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import vn.edu.fpt.xml.hrm.pojo.Timesheet;

/**
 *
 * @author Cimela
 */
public class SAXTimesheetHandler extends DefaultHandler {

    List ts;
    String tagPrefix;
    String id;
    String empID;
    String date;
    String description;
    String project;
    Double duration;
    String status;
    List listID;
    int state;
    boolean checkList;

    public SAXTimesheetHandler(String empID) {
        this.empID = empID;
        this.ts = new ArrayList();
        this.tagPrefix = "";
        this.id = "";
        this.date = "";
        this.description = "";
        this.project = "";
        this.duration = -0.1;
        this.status = "";
        this.state = 0;
        checkList = false;
    }

    public SAXTimesheetHandler(List listID) {
        this.listID = listID;
        this.ts = new ArrayList();
        this.tagPrefix = "";
        this.id = "";
        this.date = "";
        this.description = "";
        this.project = "";
        this.duration = -0.1;
        this.status = "";
        this.state = 1;
        checkList = true;
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

        if (qName.equals("timesheet")) {
            this.id = attributes.getValue("id");
            if (checkList) {
                if (listID.contains(this.id)) {
                    state = 1;
                } else {
                    state = 2;
                }
            }
        }
        tagPrefix = qName;

    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        tagPrefix = "";
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {

        switch (state) {
            case 0:
                if (tagPrefix.equals("employeeid")) {
                    String str = new String(ch, start, length);
                    if (checkList) {
                        empID = str.trim();
                        break;
                    } else {
                        if (empID.equals(str.trim())) {
                            state = 1;
                            break;
                        }
                    }
                }
            case 1:
                if (tagPrefix.equals("date")) {
                    String str = new String(ch, start, length);
                    String[] temp = str.trim().split("-");
                    this.date = temp[2] + "/" + temp[1] + "/" + temp[0];
                    break;
                }

                if (tagPrefix.equals("description")) {
                    String str = new String(ch, start, length);
                    this.description = str.trim();
                    break;
                }

                if (tagPrefix.equals("project")) {
                    String str = new String(ch, start, length);
                    this.project = str.trim();
                    break;
                }

                if (tagPrefix.equals("duration")) {
                    String str = new String(ch, start, length);
                    this.duration = Double.parseDouble(str.trim());
                    break;
                }

                if (tagPrefix.equals("status")) {
                    String str = new String(ch, start, length);
                    this.status = str.trim();
                    this.ts.add(new Timesheet(id, empID, date, description, project, duration, status));
                    /**Reset state**/
                    if (!checkList) {
                        state = 0;
                    }
                    break;
                }

            case 2:
                break;
        }
    }

    public List getTs() {
        return ts;
    }
}
